package com.example.mvvmdemo.activity.room;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ObservableField;

import com.example.mvvmdemo.R;
import com.example.mvvmdemo.databinding.ActivityDataRoomBinding;
import com.example.mvvmdemo.database.room.mtv.Mv;
import com.example.mvvmdemo.database.room.mtv.MvDao;
import com.example.mvvmdemo.database.room.mtv.MvDataBase;

import java.util.Arrays;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * User: Chentuisi
 * Date: 2023/8/9
 */
public class DataRoomActivity extends AppCompatActivity {
    public final ObservableField<String> data = new ObservableField<>();
    private ActivityDataRoomBinding binding;
    private MvDao mvDao;
    public static ExecutorService severThreadExecutor(){
        return new ThreadPoolExecutor(
                1,
                1,
                0L,
                TimeUnit.MILLISECONDS,
                new LinkedBlockingDeque<Runnable>());
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_data_room);
        binding.setLifecycleOwner(this);
        binding.setHandler(this);
        initViewModel();
        initView();
    }

    private void initViewModel(){

    }

    private void initView(){
        mvDao = MvDataBase.getInstance(this,"mv_database").mvDao();
        queryData();
    }

    //Room数据库使用
    public void insertData(){
        DataRoomActivity.severThreadExecutor().execute(() -> {
            mvDao.insertData(new Mv("12132","1","2023"));
            mvDao.insertData(new Mv("13423","2","2023"));
            getdata();
        });
    }
    public void deleteData(){
        DataRoomActivity.severThreadExecutor().execute(() -> {
            mvDao.deleteAll();
            getdata();
        });
    }
    public void updateData(){
        DataRoomActivity.severThreadExecutor().execute(() -> {
            mvDao.updateDataById(1,"1","1","1");
            getdata();
        });
    }
    public void queryData(){
        DataRoomActivity.severThreadExecutor().execute(this::getdata);
    }

    public void getdata(){
        data.set(Arrays.toString(mvDao.queryAllData().toArray()));
    }
}
